package com.example.covid19.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;

public class ParsePoint {

    public static void main(String[] args) throws Exception {
        String data = "[\"124.37553,40.18385\",\"124.37433,40.18520\",\"124.36995,40.18449\",\"124.36912,40.18495\",\"124.36794,40.18415\",\"124.36740,40.18452\",\"124.36641,40.18449\",\"124.36643,40.18384\",\"124.36679,40.18378\",\"124.36673,40.18242\",\"124.36797,40.18179\",\"124.36897,40.18197\",\"124.36948,40.18163\"]";

        List<Point> points = parsePoints(data);

        System.out.println(points);
    }

    public static class Point {
        double lng;
        double lat;

        public Point(double lng, double lat) {
            this.lng = lng;
            this.lat = lat;
        }

        @Override
        public String toString() {
            return "{" +
                    "lng:" + lng +
                    ", lat:" + lat +
                    '}';
        }
    }

    static List<Point> parsePoints(String data) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();

        List<Point> points = new ArrayList<>();
        String[] arr = objectMapper.readValue(data, String[].class);
        for (String pointStr : arr) {
            String[] pointArr = pointStr.split(",");
            double lng = Double.parseDouble(pointArr[0]);
            double lat = Double.parseDouble(pointArr[1]);
            points.add(new Point(lng, lat));
        }

        return points;
    }
}
